package com.ymatou.iapi.trading.userquery.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.AgreeRefundReq;
import com.ymatou.iapi.optrade.parameter.req.ApplyRefundReq;
import com.ymatou.iapi.optrade.parameter.vo.SalesRefundInfoVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.AgreeRefundCall;
import com.ymatou.iapi.optrade.service.BargainGroupService;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.synctrade.parameter.mongo.Order;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.userquery.parameter.resp.GetBuyerOrderInfoV2Resp;
import com.ymatou.iapi.trading.userquery.service.GetBuyerOrderInfoV2Call;
import com.ymatou.query.trading.api.parameter.GetBuyerOrderInfoBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.common.order.userquery.OrderInfoBuilder;
import com.ymttest.common.order.userquery.model.vo.BargainGroupInfo;
import com.ymttest.common.order.userquery.model.vo.OrderInfo;
import com.ymttest.database.mongo.OrderQueryWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

/**
 * 获取买家订单列表
 */
@TestSuite
public class Ts_GetBuyerOrderInfoV2 {

	private static GetBuyerOrderInfoBean req;
	private static GetBuyerOrderInfoV2Call getBuyerOrderInfoV2Call;
	
	private static Integer userId = 20238699;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买家订单列表");
	}

	@Before
	public void caseUp() {
		req = new GetBuyerOrderInfoBean();
		getBuyerOrderInfoV2Call = new GetBuyerOrderInfoV2Call();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getBuyerOrderInfoV2Call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void constructExpectResponse(GetBuyerOrderInfoV2Resp resp) {
		OrderQueryWapper orderQuery = new OrderQueryWapper();
		Order order = orderQuery.findById(req.getOrderId());

		if (order != null && order.getUserId().equals(req.getUserId())) {
			resp.setOrderInfo(OrderInfoBuilder.convertFromOrder(order, true));
		}
	}

	private void verify() {
		GetBuyerOrderInfoV2Resp expectResponse = new GetBuyerOrderInfoV2Resp();
		constructExpectResponse(expectResponse);
		GetBuyerOrderInfoV2Resp actualResponse = getBuyerOrderInfoV2Call
				.getBuyerOrderInfoResponseV2();

		OrderInfo expect = expectResponse.getOrderInfo();
		OrderInfo actual = actualResponse.getOrderInfo();
		expect.verify(actual);
		if (actual != null) {
			BargainGroupInfo act = actual.getBargainGroup();
			BargainGroupInfo exp = new Ts_GetBuyerOrderInfosV2()
					.getBargainGroupInfoFromMssql(expect.getOrderId());
			if (exp == null) {
				DataUtil.verify(
						null,
						act,
						String.format(
								"验证 sqlserver中订单id:%d 没有参加砍价团(orderInfos.bargainGroup == null)",
								expect.getOrderId()));
			} else {
				DataUtil.verifyObject(exp, act, "砍价团信息orderInfos.bargainGroup");
			}
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_001() {
		Logger.start(true, "获取买家订单列表, 参数全传，查询成功");
		try {
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			long userId = placeOrder.getUserId();

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_002() {
		Logger.start(true, "已付款,获取买家订单列表, 参数全传，查询成功");
		try {

			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long userId = placeOrder.getUserId();

			long tradingId = placeOrder.getTradingId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			// 支付订单
			TradingCallService.payOrder(userId, placeOrder.getPayableAmount(),
					tradingId);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_003() {
		Logger.start(true, "订单状态为3 取消订单,根据主单号获取订单详情");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrder = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			long userId = placeOrder.getUserId();
			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			// 取消订单
			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_004() {
		Logger.start(true, "订单状态为 接单,根据主单号获取订单详情");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrder = TradingCallService.orderAccept();
			long userId = placeOrder.getUserId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();

			TradingSyncService.getSyncResult(orderId);
			Logger.comment("前置数据准备结束");

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_005() {
		Logger.start(true, "订单状态为 发货,根据主单号获取订单详情");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long userId = placeOrderResp.getUserId();

			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_006() {
		Logger.start(true, "订单状态为 确认收货根据主单号获取订单详情");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			TradingCallService.confirmReceive(userId, orderId);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_007() {
		Logger.start(true, "订单状态为 申请退款根据主单号获取订单详情");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			com.ymatou.iapi.optrade.resp.Order order = placeOrderResp
					.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			Long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			// 支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			RefundService.applyRefund(order);

			Thread.sleep(5000);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_008() {
		Logger.start(true, "订单状态为 退款成功 根据主单号获取订单详情");
		try {
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			com.ymatou.iapi.optrade.resp.Order order = placeOrderResp
					.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			Long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			// 支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);

			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			TradingCallService.confirmReceive(userId, orderId);
			TradingSyncService.getSyncResult(orderId);

			ApplyRefundReq applyRefundReq = RefundService.applyRefund(order);

			Thread.sleep(5000);

			AgreeRefundReq salesrefundsuccessReq = new AgreeRefundReq();
			AgreeRefundCall salesrefundsuccessCall = new AgreeRefundCall();

			salesrefundsuccessReq.setOrderId(orderId);

			int i = 0;
			for (SalesRefundInfoVo salesRefundInfoVo : applyRefundReq
					.getSalesRefundInfoList()) {

				salesrefundsuccessReq.setRefundBillNo(salesRefundInfoVo
						.getRefundBillNo());
				salesrefundsuccessCall.setData(salesrefundsuccessReq);

				Logger.comment(String.format("orderid: %s 第 %s 个商品 退款执行",
						orderId, i + 1));

				salesrefundsuccessCall.callService();

				Logger.verifyEquals("true",
						salesrefundsuccessCall.getString("success"),
						"验证退货退款成功返回");
				i++;
			}

			Thread.sleep(5000);

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_009() {
		Logger.start(true, "买家用户id为null");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			DataUtil.verify(400, getBuyerOrderInfoV2Call.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_010() {
		Logger.start(true, "订单状态为 发货,根据订单号获取订单详情");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long userId = placeOrderResp.getUserId();

			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			req.setUserId(userId);
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_011() {
		Logger.start(true, "验证砍价团信息，查询成功, 团长开团 组团失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			// 砍价团id
			String bizId = TestDataManager.getRandomNum(9);

			TradingServiceResp placeOrderResp = BargainGroupService
					.openGroup(bizId);

			// 参团
			TradingServiceResp placeOrder2 = BargainGroupService
					.joinGroup(bizId);

			long orderId = placeOrder2.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备结束");

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 2, -3);

			TradingSyncService.getSyncResult(orderId);

			req.setUserId(placeOrderResp.getUserId());
			req.setOrderId(orderId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_012() {
		Logger.start(true, "订单状态为1，推手订单,根据主单号获取订单详情");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			long hunterId = 4321;

			TradingServiceResp placeOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
					
			long userId = placeOrder.getUserId();

			long orderId = placeOrder.getOrderList().get(0).getOrderId();
			Logger.comment("前置数据准备结束");

			TradingSyncService.getSyncResult(orderId);

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

			Logger.verifyEquals(null, getBuyerOrderInfoV2Call
					.getBuyerOrderInfoResponseV2().getOrderInfo(),
					"app买家查询无法查找hunter订单");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerOrderInfoV2_013() {
		Logger.start(true, "虚拟商品下单, 参数全传，查询成功");
		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithThreeProcutInTwoOrder(false,
							BigDecimal.ZERO, userId,
							LogisticsTypeEnum.AutoDelivery);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			long userId = placeOrderResp.getUserId();

			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			req.setOrderId(orderId);
			req.setUserId(userId);

			getBuyerOrderInfoV2Call.setData(req);
			getBuyerOrderInfoV2Call.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}